const crypto = require('crypto');

const listToTree = (list, parentId = 0) => {
	return list.filter(item => item.parent_id === parentId).map(item => {
		const children = listToTree(list, item.id)

		if (children.length > 0) {
			return {
				...item,
				children
			}
		} else {
			return {
				...item
			}
		}
	})
}

exports = module.exports = {
	md5(string) {
		return crypto.createHash('md5').update(string).digest('hex') || '';
	},

	rand(min, max) {
		return Math.floor(Math.random() * (max - min + 1)) + min;
	},

	listToTree
}
